package com.hcqc.demo.controller;

import com.hcqc.demo.server.UserServer;
import com.hcqc.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    UserServer userServer;

    //登陆
    @PostMapping("/login")
    public Result login(String username, String pwd, HttpServletRequest request){
        Result result=userServer.login(username,pwd,request);
        return result;
    }

    //退出
    @RequestMapping("/logout")
    public void logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
    }

    //是否登录
    @RequestMapping("/show")
    public Result show(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object user_name = session.getAttribute("USER_NAME");
        if (user_name!=null)
            return Result.success((String) user_name,null,0);
        return Result.fail("请登录");
    }
}
